<?php
namespace App\Service\Admin;

class ItemService1001 extends BaseService
{
    //数据列表
    public static function index($pas = [], $pages)
    {
        $start = ($pages['start'] - 1) * $pages['limit'];
        $db = self::dbConn(self::$dbConfigItem)->where('del', 0);
//        if (isset($pas['role_id']) && $pas['role_id']) {
//            $db = $db->where('role_id', $pas['role_id']);
//        }
//        if (isset($pas['depart_id']) && $pas['depart_id']) {
//            $db = $db->where('depart_id', $pas['depart_id']);
//        }
        $total = $db->count();
        $res = $db->skip($start)->take($pages['limit'])->get();
        if (!count($res)) {
            return self::toback(-2, '数据不存在');
        }
        $statuss = self::$itemStatuss;
        $arys = [];
        foreach ($res as $v) {
            $leaderOne = (array) self::dbConn(self::$dbStaff)->where('id', $v->leader_id)->first();
            $leader_name = empty($leaderOne) ? '' : $leaderOne['title'];
            $status_name = array_key_exists($v->status, $statuss) ? $statuss[$v->status] : '';
            $teamOnes = self::dbConn(self::$dbConfigTeam)
                ->where('del', 0)
                ->where('item_id', $v->id)
                ->get();
            $teams = [];
            if (count($teamOnes)) {
                foreach ($teamOnes as $vv) {
                    $teams[] = $vv->staff_name;
                }
            }
            $teamstr = count($teams) ? implode(',', $teams) : '-';
            $departOne = (array) self::dbConn(self::$dbStDepart)
                ->where('id', $v->depart_id)
                ->first();
            $depart_name = empty($departOne) ? '' : $departOne['title'];
            $arys[] = [
                'id' => $v->id, 'title' => $v->title,
                'starttime' => $v->starttime, 'endtime' => $v->endtime,
                'leader_id' => $v->leader_id, 'leader_name' => $leader_name,
                'status' => $v->status, 'status_name' => $status_name,
                'teams' => $teams, 'teamstr' => $teamstr,
                'lasttime' => $v->uptime,
                'depart_name' => $depart_name,
                'is_able' => $v->is_able,
            ];
        }
        return self::toback(0, '', $arys, $total);
    }

    //选项
    public static function getOption($pas)
    {
        $start = isset($pas['start'])&&$pas['start'] ? $pas['start'] : '';
        $end = isset($pas['end'])&&$pas['end'] ? $pas['end'] : '';
        $leader_id = isset($pas['leader_id'])&&$pas['leader_id'] ? $pas['leader_id'] : '';
        $arys = [
            'starttime' => $start, 'start' => [
                'y' => '', 'm' => '', 'd' => '',
                'h' => '', 'i' => '',
            ],
            'endtime' => $start, 'end' => [
                'y' => '', 'm' => '', 'd' => '',
                'h' => '', 'i' => '',
            ],
            'leaderList' => [],
            'leader_id' => $leader_id,
        ];
        if (!$start) {
            $start = toDate();
        }
        $starts = explode(' ', $start);
        $riqis = explode('-', $starts[0]);
        $shijians = explode(':', $starts[1]);
        $arys['start']['y'] = $riqis[0];
        $arys['start']['m'] = $riqis[1];
        $arys['start']['d'] = $riqis[2];
        $arys['start']['h'] = $shijians[0];
        $arys['start']['i'] = $shijians[1];
        if (!$end) {
            $end = toDate();
        }
        $ends = explode(' ', $end);
        $riqis = explode('-', $ends[0]);
        $shijians = explode(':', $ends[1]);
        $arys['end']['y'] = $riqis[0];
        $arys['end']['m'] = $riqis[1];
        $arys['end']['d'] = $riqis[2];
        $arys['end']['h'] = $shijians[0];
        $arys['end']['i'] = $shijians[1];
        $arys['leaderList'] = self::dbConn(self::$dbStaff)
            ->select('id', 'title')
            ->where('id', '>', 1)
            ->where('is_able', 0)
            ->get();
        $arys['departList'] = self::dbConn(self::$dbStDepart)
            ->select('id', 'title')
            ->where('del', 0)
            ->get();
        return self::toback(0, '', $arys);
    }

    //新增
    public static function add($pas)
    {
        $pas['addtime'] = $pas['uptime'] = toDate();
        $res = self::dbConn(self::$dbConfigItem)->insertGetId($pas);
        if (!$res) {
            return self::toback(-1, 'error');
        }
        return self::toback();
    }

    //修改
    public static function modify($id, $pas)
    {
        $res = self::dbConn(self::$dbConfigItem)->where('id', $id)->first();
        if (empty($res)) {
            return self::toback(-2, '数据不存在');
        }
        $pas['uptime'] = toDate();
        $res = self::dbConn(self::$dbConfigItem)->where('id', $id)->update($pas);
        if (!$res) {
            return self::toback(-1, 'error');
        }
        return self::toback();
    }

    //删除
    public static function del($id)
    {
        $res = self::dbConn(self::$dbConfigItem)->where('id', $id)->first();
        if (empty($res)) {
            return self::toback(-2, '数据不存在');
        }
        $res = self::dbConn(self::$dbConfigItem)
            ->where('id', $id)
            ->update(['del' => 1, 'deltime' => toDate()]);
        if (!$res) {
            return self::toback(-1, 'error');
        }
        return self::toback();
    }

    //团队获取
    public static function teamGet($id)
    {
        $arys['staff'] = self::dbConn(self::$dbStaff)
            ->select('id', 'title')
            ->where('is_able', 0)
            ->where('id', '>', 1)
            ->get();
        $arys['team'] = self::dbConn(self::$dbConfigTeam)
            ->select('id', 'staff_name')
            ->where('del', 0)
            ->where('item_id', $id)
            ->get();
        return self::toback(0, '', $arys);
    }

    //团队设置
    public static function teamSet($id, $staff_id)
    {
        $res = self::dbConn(self::$dbConfigTeam)
            ->where('del', 0)
            ->where('item_id', $id)
            ->where('staff_id', $staff_id)
            ->first();
        if ($res) {
            return self::toback(-2, '该人员已经配置了');
        }
        $staffOne = self::dbConn(self::$dbStaff)
            ->where('id', $staff_id)
            ->first();
        if (!$staffOne) {
            return self::toback(-2, '该人员不存在');
        }
        $res = self::dbConn(self::$dbConfigTeam)->insertGetId([
            'item_id' => $id, 'addtime' => toDate(),
            'staff_id' => $staff_id, 'staff_name' => $staffOne->title,
        ]);
        if (!$res) {
            return self::toback(-1, 'error');
        }
        return self::toback();
    }

    //团队移除
    public static function teamDel($id)
    {
        $res = self::dbConn(self::$dbConfigTeam)
            ->where('del', 0)
            ->where('id', $id)
            ->first();
        if (!$res) {
            return self::toback(-2, '该人员未配置');
        }
        $res = self::dbConn(self::$dbConfigTeam)
            ->where('del', 0)
            ->where('id', $id)
            ->first();
        if (!$res) {
            return self::toback(-2, '该人员未配置');
        }
        $res = self::dbConn(self::$dbConfigTeam)->where('id', $id)
            ->update([
                'del' => 1, 'deltime' => toDate(),
            ]);
        if (!$res) {
            return self::toback(-1, 'error');
        }
        return self::toback();
    }

    //禁用启用
    public static function ableSet($id, $pas)
    {
        $res = self::dbConn(self::$dbConfigItem)
            ->where('del', 0)
            ->where('id', $id)
            ->first();
        if (!$res) {
            return self::toback(-2, '没有数据');
        }
        self::dbConn(self::$dbConfigItem)
            ->where('id', $id)
            ->update($pas);
        return self::toback();
    }
}